package com.fz.song.controller.admin;

import com.fz.song.config.BlogConfig;
import com.fz.song.pojo.BlogAdmin;
import com.fz.song.service.IBlogAdminService;
import com.fz.song.service.impl.BlogAdminServiceImpl;
import com.fz.song.util.ResultObj;
import com.fz.song.util.UUIDUtil;
import com.fz.song.util.WangEditorResult;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by sfz on 2018/5/23.
 * 主页controller
 */
@Controller
@RequestMapping("/file")
public class FileController {
    private static Log logger = LogFactory.getLog(FileController.class);

    @RequestMapping("/upload")
    @ResponseBody
    public Object fileupload(HttpServletRequest request, @RequestParam("file") MultipartFile file) {
        logger.info("开始文件上传");
        if (!file.isEmpty()) {//文件不为空
            try {
                String rootpath = request.getServletContext().getRealPath("/");
                String father = createPath(rootpath, "banner");
                String filename = file.getOriginalFilename();
                String filetype = filename.substring(filename.lastIndexOf("."), filename.length());
                String filenamenew = UUIDUtil.getUUID() + "." + filetype;
                File file1 = new File(rootpath + father, filenamenew);
                file.transferTo(file1);
                String finalPath = father + File.separator + filenamenew;
                return ResultObj.ok("文件上传成功!", finalPath);
            } catch (IOException e) {
                e.printStackTrace();
                logger.error("开始文件失败");
                logger.error(e.getMessage());
                return ResultObj.error("文件上传失败!");
            }
        } else {
            logger.info("文件为空");
            return ResultObj.error("文件为空!");
        }

    }

    private String createPath(String rootpath, String type) {
        SimpleDateFormat sdf = new SimpleDateFormat(BlogConfig.FILE_FMT);
        String format = sdf.format(new Date());
        File file = new File(rootpath, "static"+File.separator+"data" + File.separator + type + File.separator + format);
        if (!file.exists()) {
            file.mkdirs();
        }
        return File.separator + "static"+File.separator+"data" + File.separator + type + File.separator + format;
    }


    @RequestMapping("/wangEditorImgFile")
    @ResponseBody
    public Object wangEditorImgFile(HttpServletRequest request, @RequestParam("file") MultipartFile file) {
        logger.info("开始文件上传");
        if (!file.isEmpty()) {//文件不为空
            try {
                String rootpath = request.getServletContext().getRealPath("/");
                String father = createPath(rootpath, "bloginfo");
                String filename = file.getOriginalFilename();
                String filetype = filename.substring(filename.lastIndexOf("."), filename.length());
                String filenamenew = UUIDUtil.getUUID() + "." + filetype;
                File file1 = new File(rootpath + father, filenamenew);
                file.transferTo(file1);
                String finalPath = father + File.separator + filenamenew;
                ArrayList<String> strings = new ArrayList<>();
                strings.add(finalPath);
                WangEditorResult wangEditorResult = new WangEditorResult(0, strings);
                return wangEditorResult;
            } catch (IOException e) {
                e.printStackTrace();
                logger.error("开始文件失败");
                logger.error(e.getMessage());
                WangEditorResult wangEditorResult = new WangEditorResult(-1, null);
                return wangEditorResult;
            }
        } else {
            logger.info("文件为空");
            WangEditorResult wangEditorResult = new WangEditorResult(0 - 1, null);
            return wangEditorResult;
        }

    }
}
